package zhai.docking.alipay.service.base.alipay.api;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenSpBlueseaactivityCreateRequest;
import com.alipay.api.request.AlipayOpenSpBlueseaactivityModifyRequest;
import com.alipay.api.request.AlipayOpenSpBlueseaactivityQueryRequest;
import com.alipay.api.response.AlipayOpenSpBlueseaactivityCreateResponse;
import com.alipay.api.response.AlipayOpenSpBlueseaactivityModifyResponse;
import com.alipay.api.response.AlipayOpenSpBlueseaactivityQueryResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhai.docking.alipay.constant.AlipayConstant;
import zhai.docking.alipay.constant.LogConstant;
import zhai.docking.alipay.domain.base.alipay.*;
import zhai.docking.alipay.domain.base.alipay.common.BaseAlipayCommonOut;
import zhai.docking.alipay.domain.base.bizlog.BizLogDataTemp;
import zhai.docking.alipay.domain.base.bizlog.BizLogSave;
import zhai.docking.alipay.prop.AlipayApiProperties;
import zhai.docking.alipay.service.base.bizlog.BizLogSer;
import zhai.docking.alipay.util.JSONUtil;
import zhai.docking.alipay.util.ObjectUtil;
import zhai.docking.alipay.util.StringUtil;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.service.base.alipay.api
 * @project-name zhai-docking-alipay
 * @description 基础-支付宝-新蓝海 服务实现
 * @create-datetime 2020/12/30 11:26
 */
@Slf4j
@Service
public class BaseAlipayApiBlueSeaSerImpl implements BaseAlipayApiBlueSeaSer {

    private static final String BUSINESS_NAME = "【基础-支付宝-新蓝海 服务】";
    private static final String METHOD_QUERY = "（alipay.open.sp.blueseaactivity.query(服务商返佣活动申请单详情查询) https://opendocs.alipay.com/apis/01ebih）";
    private static final String METHOD_CREATE = "（alipay.open.sp.blueseaactivity.create(服务商返佣活动报名申请) https://opendocs.alipay.com/apis/01ebig）";
    private static final String METHOD_MODIFY = "（alipay.open.sp.blueseaactivity.modify(服务商返佣活动申请单修改) https://opendocs.alipay.com/apis/01e46n）";

    @Autowired
    AlipayApiProperties alipayApiProp;
    @Autowired
    BizLogSer bizLogSer;

    @SneakyThrows
    @Override
    public BaseAlipayBlueSeaQueryOut query(BaseAlipayBlueSeaQueryIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_QUERY, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayOpenSpBlueseaactivityQueryRequest request = new AlipayOpenSpBlueseaactivityQueryRequest ();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayOpenSpBlueseaactivityQueryResponse response = alipayClient.execute(request);
        /** 操作：结果转换 */
        BaseAlipayBlueSeaQueryOut result = ObjectUtil.copy(response, BaseAlipayBlueSeaQueryOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, request);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_QUERY, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }

    @SneakyThrows
    @Override
    public BaseAlipayBlueSeaCreateOut create(BaseAlipayBlueSeaCreateIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_CREATE, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayOpenSpBlueseaactivityCreateRequest request = new AlipayOpenSpBlueseaactivityCreateRequest ();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayOpenSpBlueseaactivityCreateResponse response = alipayClient.execute(request);
        /** 操作：记录业务日志 */
        bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.BLUE_SEA_CREATE_LOG));
        /** 操作：结果转换 */
        BaseAlipayBlueSeaCreateOut result = ObjectUtil.copy(response, BaseAlipayBlueSeaCreateOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_CREATE, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }

    @SneakyThrows
    @Override
    public BaseAlipayBlueSeaModifyOut modify(BaseAlipayBlueSeaModifyIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_MODIFY, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayOpenSpBlueseaactivityModifyRequest request = new AlipayOpenSpBlueseaactivityModifyRequest ();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayOpenSpBlueseaactivityModifyResponse response = alipayClient.execute(request);
        /** 操作：记录业务日志 */
        bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.BLUE_SEA_MODIFY_LOG));
        /** 操作：结果转换 */
        BaseAlipayBlueSeaModifyOut result = ObjectUtil.copy(response, BaseAlipayBlueSeaModifyOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_MODIFY, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }
}